package cn.gok.controller;

import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author shikundai
 * @date 2021/3/24
 * @time 11:30
 * @description: 错误控制器
 */
@RestControllerAdvice
public class ErroController {
    /**
     * 描述：其他未知错误
     *
     * @author shikundai
     * @date 2021/3/24
     * @time 14:30
     */
    @ExceptionHandler(Exception.class)
    public Map otherErro(Exception e) {
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "出现未知错误");
        map.put("obj", e.getMessage());
        return map;
    }

    /**
     * 描述：未知的账户
     *
     * @author shikundai
     * @date 2021/3/24
     * @time 14:32
     */
    @ExceptionHandler(UnknownAccountException.class)
    public Map UnknowAccount(Exception e) {
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "未知的账户");
        map.put("obj", e.getMessage());
        return map;
    }

    /**
     * 描述：密码出错
     *
     * @author shikundai
     * @date 2021/3/24
     * @time 14:32
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    public Map pswErro(Exception e) {
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "密码出错");
        map.put("obj", e.getMessage());
        return map;
    }

    /**
     * 描述：密码过期
     *
     * @author shikundai
     * @date 2021/3/24
     * @time 14:32
     */
    @ExceptionHandler(ExpiredCredentialsException.class)
    public Map pswErroExpresed(Exception e) {
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "密码过期");
        map.put("obj", e.getMessage());
        return map;
    }

    /**
     * 描述：权限异常
     *
     * @author shikundai
     * @date 2021/3/24
     * @time 14:32
     */
    @ExceptionHandler(AuthorizationException.class)
    public Map authorizationException(Exception e) {
        Map map = new HashMap();
        map.put("code", 200);
        map.put("msg", "你没有该权限");
        map.put("obj", e.getMessage());
        return map;
    }
}
